/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.weblayer.controllers;

import cz.muni.fi.pa165.machineryrental.weblayer.custommodels.User;
import cz.muni.fi.pa165.machineryrental.weblayer.utils.DBUtil;
import cz.muni.fi.pa165.machineryrental.weblayer.validator.UserValidator;
import java.sql.SQLException;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author jostri
 */
@Controller
public class LoginController {

	@Autowired
	private DBUtil dbUtil;
	
	@Autowired
	private UserValidator userValidator;
	
	@InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.setValidator(this.userValidator);
    }
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(ModelMap model) {

		return new ModelAndView("/login.jsp");
	}

	@RequestMapping(value = "/loginerror", method = RequestMethod.GET)
	public ModelAndView loginError(ModelMap model) {
		
		ModelAndView mav = new ModelAndView("/login.jsp");
		mav.addObject("error", "true");
		
		return mav;

	}
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/index", method = RequestMethod.GET)
	public ModelAndView index() throws SQLException {
		
		List<User> users = this.dbUtil.getUsers();
		System.out.println("User count: " + users.size());
		
		return new ModelAndView("/login/index.jsp","users", users);
	}
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/add", method = RequestMethod.GET)
	public ModelAndView add() {
		User u = new User();
		u.setEnabled(true);
		
		return new ModelAndView("/login/add.jsp", "user", u);
	}
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/add", method = RequestMethod.POST)
    public ModelAndView add(@Valid @ModelAttribute("user") User user, BindingResult result) throws SQLException {

        if (result.hasErrors()) {
			return new ModelAndView("/login/add.jsp", "user", user);
		}
		
		this.dbUtil.create(user);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/useradministration/index");
        return mav;
    }
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/edit/{id}", method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable int id) throws SQLException {
		
		User u = this.dbUtil.findUserById(id);
		
		return new ModelAndView("/login/edit.jsp", "user", u);
	}
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/edit", method = RequestMethod.POST)
    public ModelAndView edit(@Valid @ModelAttribute("user") User user, BindingResult result) throws SQLException {

        if (result.hasErrors()) {
			return new ModelAndView("/login/edit.jsp", "user", user);
		}
		
		this.dbUtil.update(user);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/useradministration/index");
        return mav;
    }
	
        @Secured("ROLE_ADMIN")
	@RequestMapping(value = "/useradministration/remove/{id}", method = RequestMethod.GET)
	public ModelAndView remove(@PathVariable int id) throws SQLException {
		
		this.dbUtil.remove(id);
		
		return new ModelAndView("redirect:/useradministration/index");
	}
}
